#ifndef SYMBOL_TABLE_H
#define SYMBOL_TABLE_H

#include "main.h"
#include "semantic_bridge.h"

// 循环依赖
typedef struct Type Type;
typedef struct Symbol Symbol;

typedef enum {
    INT_TYPE,
    FLOAT_TYPE
} BasicType;

typedef enum { 
    SYM_VARIABLE, 
    SYM_FUNCTION, 
    SYM_STRUCT 
} SymbolType;

// 类型系统
struct Type {
    enum { BASIC, ARRAY, STRUCTURE, FUNCTION } kind;
    union {
        BasicType basic; // 0 for int, 1 for float
        struct { struct Type* elem; int size; } array;
        struct { char* name; Symbol* fields; } structure;
        struct { struct Type* ret; Symbol* params; } function;
    };
    struct Type* next; // 添加链表指针
};

struct Symbol {
    char* name;          // 符号名称
    SymbolType type;     // 符号类型
    int lineno;          // 声明行号
    Type* type_info;     // 类型属性（指向Type结构）
    struct Symbol* next; // 链表指针
};

typedef struct Scope {
    Symbol* symbols;     // 当前作用域的符号
    struct Scope* parent;// 父作用域
} Scope;

extern Scope* current_scope; // 添加extern声明

// 作用域管理
void enter_scope();
void exit_scope();

// 符号表操作
void add_symbol(char* name, SymbolType type, Type* type_info, int lineno);
Symbol* find_symbol(char* name);
int check_symbol_conflict(char* name);

// Type
void free_type(Type* type);

#endif